package tom.vertx.core.demo10;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.dns.DnsClient;
import io.vertx.core.dns.MxRecord;
import io.vertx.core.dns.SrvRecord;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @author ZHUFEIFEI
 */
public class DnsClientDemo {

  public static void main(String[] args) {
    Vertx.vertx().deployVerticle(new DnsV());
  }

  static class DnsV extends AbstractVerticle {
    @Override
    public void start(Promise<Void> startPromise) {
      //114dns server
//      DnsClient client = vertx.createDnsClient(53, "114.114.114.114");
      //默认获取本地dns服务器地址，
      DnsClient client = vertx.createDnsClient();

      vertx.executeBlocking(b -> {
        try {
          //获取dns服务器信息
          Field field = client.getClass().getDeclaredField("dnsServer");
          field.setAccessible(true);
          System.out.println(field.get(client));
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (NoSuchFieldException e) {
          e.printStackTrace();
        }
      }, rs -> {

      });

      //相当于nslookup
      client.lookup("vertx.io", ar -> {
        if (ar.succeeded()) {
          System.out.println(ar.result());
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });

      //ipv4 A记录
      client.lookup4("baidu.com", ar -> {
        if (ar.succeeded()) {
          System.out.println(ar.result());
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });

      //相当于dig
      client.resolveA("baidu.com", ar -> {
        if (ar.succeeded()) {
          List<String> records = ar.result();
          for (String record : records) {
            System.out.println(record);
          }
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
      //ipv6
      client.resolveAAAA("baidu.com", ar -> {
        if (ar.succeeded()) {
          List<String> records = ar.result();
          for (String record : records) {
            System.out.println(record);
          }
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
      //cname解析
      client.resolveCNAME("vertx.io", ar -> {
        if (ar.succeeded()) {
          List<String> records = ar.result();
          for (String record : records) {
            System.out.println(record);
          }
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
      //mail server解析
      client.resolveMX("163.com", ar -> {
        if (ar.succeeded()) {
          List<MxRecord> records = ar.result();
          for (MxRecord record: records) {
            System.out.println(record);
          }
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
      //附加信息解析
      client.resolveTXT("vertx.io", ar -> {
        if (ar.succeeded()) {
          List<String> records = ar.result();
          for (String record: records) {
            System.out.println(record);
          }
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
      //dns服务器信息
      client.resolveNS("vertx.io", ar -> {
        if (ar.succeeded()) {
          List<String> records = ar.result();
          for (String record: records) {
            System.out.println(record);
          }
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
      //服务器附加信息
      client.resolveSRV("vertx.io", ar -> {
        if (ar.succeeded()) {
          List<SrvRecord> records = ar.result();
          for (SrvRecord record: records) {
            System.out.println(record);
          }
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
      //ptr
      client.resolvePTR("1.0.0.10.in-addr.arpa", ar -> {
        if (ar.succeeded()) {
          String record = ar.result();
          System.out.println(record);
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
      //根据ip地址反向查找
      client.reverseLookup("10.0.0.1", ar -> {
        if (ar.succeeded()) {
          String record = ar.result();
          System.out.println(record);
        } else {
          System.out.println("Failed to resolve entry" + ar.cause());
        }
      });
    }
  }

}
